package com.tool.toollibrary.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Matrix;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;

import com.tool.mylibrary.utils.Logger;

/**
 * 练习 手势的监听，，按下 离开，滑动双击。。。以及缩放。。缩放前 缩放后等方法。。
 * 控件不做处理。
 * Created by Michael on 2017/10/10.
 */

@SuppressLint("AppCompatCustomView")
public class MyView extends ImageView  implements View.OnTouchListener {

    public static final String  TAG="myview_";
    private Context mContext;
    private GestureDetector mGestureDetector;
    private ScaleGestureDetector mScaleDetector;

    public MyView(Context context) {
        super(context);
        initData(context);
    }



    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initData(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context);
    }

    private void initData(Context context) {
        this.mContext=context;
        super.setOnTouchListener(this);
        super.setClickable(true);
        super.setLongClickable(true);
        super.setFocusable(true);
       // mGestureDetector = new GestureDetector(mContext,new MyGestureListener());
        //mGestureDetector.setOnDoubleTapListener(new MyGestureListener());
        mScaleDetector = new ScaleGestureDetector(mContext,new MyScaleGestureListener());
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //return mGestureDetector.onTouchEvent(motionEvent);
        return mScaleDetector.onTouchEvent(motionEvent);
    }


    //手势监听类

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {


        public MyGestureListener() {
            super();
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Logger.d(TAG,"---onSingleTapUp");
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Logger.d(TAG,"---onLongPress");

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Logger.d(TAG,"---onScroll");
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Logger.d(TAG,"---onFling");
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Logger.d(TAG,"---onShowPress");

        }

        @Override
        public boolean onDown(MotionEvent e) {
            Logger.d(TAG,"---onDown");
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Logger.d(TAG,"---onDoubleTap");
            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            //Logger.d(TAG,"---onDoubleTapEvent");
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
           // Logger.d(TAG,"---onSingleTapConfirmed");
            return false;
        }
    }

    // 缩放手势监听类
    class MyScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener{

        Matrix mMatrix=new Matrix();
        //缩放时
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            Logger.d(TAG,""+detector.getScaleFactor());
            if(detector.getScaleFactor()< 2){
                return false;
            }
            mMatrix.preScale(detector.getScaleFactor(), detector.getScaleFactor());
            setImageMatrix(mMatrix);
            return true;
        }

        //缩放开始
        @Override
        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            Logger.d(TAG,"onScaleBegin");
            return true;
        }

        //缩放结束
        @Override
        public void onScaleEnd(ScaleGestureDetector scaleGestureDetector) {
            Logger.d(TAG,"onScaleEnd");
        }
    }
}
